/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package weka;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.core.converters.CSVLoader;


/**
 *
 * @author Utilisateur
 */
public class xsl2csvAction extends AbstractAction{
    
    private MainWindow fenetre;
    
    public xsl2csvAction(MainWindow fenetre, String texte)
    {
        super(texte);
        this.fenetre = fenetre;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String path = "";
        String filename = "";
        String testS = "";
        MonFiltre arff = new MonFiltre( 
           new String[]{"xls"},
           "Fichiers Excel (*.xls)");
        JFileChooser choix = new JFileChooser();
        choix.addChoosableFileFilter(arff);
        int retour=choix.showOpenDialog(fenetre);
        if(retour==JFileChooser.APPROVE_OPTION){
           // un fichier a été choisi (sortie par OK)
           // nom du fichier  choisi 
           //fenetre.getTextField().setText(choix.getSelectedFile().getName());
           // filename = choix.getSelectedFile().getName();
           // chemin absolu du fichier choisi
           //fenetre.getEtude().setExcelPath(choix.getSelectedFile().getPath());
           //path = choix.getSelectedFile().getPath();
           testS= choix.getSelectedFile().getAbsolutePath();
           //choix.getSelectedFile().
        }
        //TestXLS test = new TestXLS(fenetre.getEtude().getExcelPath()); 
        TestXLS test = new TestXLS(testS);
        test.lecture(); 
        //test.ecrireCSV(fenetre.getEtude().getExcelPath()+".csv");
        File f = new File(testS);
       filename = (f.getName() != null) ? f.getName().substring(0,f.getName().indexOf('.')) : "";
       path=filename+".csv";
        test.ecrireCSV(path);
        
        CSVLoader loader = new CSVLoader();
        try {
            loader.setSource(new File(path));
        } catch (IOException ex) {
            Logger.getLogger(xsl2csvAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        Instances data = null;
        try {
            //data = loader.getDataSet();
            data = loader.getStructure();
        } catch (IOException ex) {
            Logger.getLogger(xsl2csvAction.class.getName()).log(Level.SEVERE, null, ex);
        }
 
        // save ARFF
        ArffSaver saver = new ArffSaver();
        saver.setInstances(data);
        try {
            saver.setFile(new File(filename+".arff"));
        } catch (IOException ex) {
            Logger.getLogger(xsl2csvAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            saver.setDestination(new File(filename+".arff"));
        } catch (IOException ex) {
            Logger.getLogger(xsl2csvAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            saver.writeBatch();
        } catch (IOException ex) {
            Logger.getLogger(xsl2csvAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        //File f = new File(testS+".csv");
        //f.delete();
    
    }
}
